---
title: "Replication Archive for *Cross-Partisan Conversation Reduced Affective Polarization for
Republicans and Democrats Even After the Contentious 2020 Election*"
author: "Erin Rossiter and Taylor Carlson"
date: "9/11/2023"
output:
    pdf_document:
        toc: true
header-includes:
  - \usepackage{booktabs}
  - \usepackage{siunitx}
  - \usepackage{pdflscape}
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = T, eval = T, warning = F)
```

# Setup

## Required R packages

```{r}
## Install these if needed

# install.packages("tinytex")
# tinytex::install_tinytex()
#install.packages("modelsummary")
#install.packages("effectsize")
#install.packages("pBrackets")
#install.packages("estimatr")
#install.packages("xtable")
#install.packages("Hmisc")
#install.packages("quanteda")
#install.packages("kableExtra")
#install.packages("tidyverse")
#install.packages("ggplot2")

## Load these
library(tidyverse, quietly = T)
library(ggplot2)
```

## Current environment

```{r}
version
pkgs <- c(
  "modelsummary",
  "effectsize",
  "pBrackets",
  "estimatr",
  "xtable",
  "Hmisc",
  "quanteda",
  "kableExtra",
  "tidyverse",
  "ggplot2"
)
vsns <- lapply(pkgs, packageVersion)
names(vsns) <- pkgs
vsns
```


## Load data

```{r}
results <- readRDS("experimentdata.RDS")
```

## Helper function

```{r}
maketable <- function(mod_list, title){
  modelsummary::modelsummary(
    mod_list,
    fmt = 2,
    title = title,
    stars = c("*" = .05),
    output = "latex",
    gof_map = c("nobs")
  )
}
```


# Manuscript

## Descriptive statistics in 'Partisanship and Threat' section

```{r}
# Feel like a winner/loser after 2020 election
prop.table(table(results$status_winner[results$full_cluster],
                 results$pid2[results$full_cluster]), 2)

# Which party has higher status right now?
prop.table(table(results$status_parties[results$full_cluster],
                 results$pid2[results$full_cluster]), 2)
```

## Main Analyses for Figure 1

```{r}
# H1
h1_affect <- estimatr::lm_robust(formula = outparty_change ~ Z,
                            data = results[results$full_cluster, ],
                            fixed_effects = ~block_id,
                            clusters = cluster_id_se)

# H2
h2_sp <- estimatr::lm_robust(formula = sp_change ~ Z,
                            data = results[results$full_cluster, ],
                            fixed_effects = ~block_id,
                            clusters = cluster_id_se)

# H3
h3_elec <- estimatr::lm_robust(formula = elec_change ~ Z,
                            data = results[results$full_cluster, ],
                            fixed_effects = ~block_id,
                            clusters = cluster_id_se)

# Wolak (exploratory)
dem_values <- estimatr::lm_robust(formula = wolak_score ~ Z,
                             data=results[results$full_cluster, ],
                             clusters = cluster_id_se,
                             fixed_effects = ~block_id)

# Outparty Talk (exploratory)
talk_elec <- estimatr:: lm_robust(formula = convo_elec_outparty_change ~ Z,
                                   data = results[results$full_cluster,],
                                   clusters = cluster_id_se,
                                   fixed_effects = ~block_id)

# H4
h4_affect <- estimatr::lm_robust(formula = outparty_change ~ Z*pid2, 
                            data = results[results$full_cluster, ],
                            clusters = cluster_id_se,
                            fixed_effects = ~block_id)

# H5
h5_sp <- estimatr:: lm_robust(formula = sp_change ~ Z*pid2, 
                            data = results[results$full_cluster, ],
                            clusters = cluster_id_se,
                            fixed_effects = ~block_id)

# H6
h6_elec <- estimatr:: lm_robust(formula = elec_change ~ Z*pid2, 
                            data = results[results$full_cluster, ],
                            clusters = cluster_id_se,
                            fixed_effects = ~block_id)

# Wolak (exploratory)
dem_values_int <- estimatr::lm_robust(formula = wolak_score ~ Z*pid2,
                             data=results[results$full_cluster, ],
                             clusters = cluster_id_se,
                             fixed_effects = ~block_id)

# Outparty Talk (exploratory)
talk_elec_int <- estimatr:: lm_robust(formula = convo_elec_outparty_change ~ Z*pid2,
                                   data = results[results$full_cluster,],
                                   clusters = cluster_id_se,
                                   fixed_effects = ~block_id)

# CATEs

# Outparty affect
cate_d_affect <- estimatr::lm_robust(outparty_change ~ Z,
                                               data = results[results$full_cluster, ],
                                               fixed_effects = ~block_id,
                                               subset = pid2 == "D")
cate_r_affect <- estimatr::lm_robust(outparty_change ~ Z,
                                               data = results[results$full_cluster, ],
                                               fixed_effects = ~block_id,
                                               subset = pid2 == "R")
# Social polarization
cate_d_sp <- estimatr::lm_robust(sp_change ~ Z,
                                               data = results[results$full_cluster, ],
                                               fixed_effects = ~block_id,
                                               subset = pid2 == "D")
cate_r_sp <- estimatr::lm_robust(sp_change ~ Z,
                                               data = results[results$full_cluster, ],
                                               fixed_effects = ~block_id,
                                               subset = pid2 == "R")
# Election integrity
cate_d_elec <- estimatr::lm_robust(elec_change ~ Z,
                                               data = results[results$full_cluster, ],
                                               fixed_effects = ~block_id,
                                               subset = pid2 == "D")
cate_r_elec <- estimatr::lm_robust(elec_change ~ Z,
                                               data = results[results$full_cluster, ],
                                               fixed_effects = ~block_id,
                                               subset = pid2 == "R")

# Wolak (exploratory)
dem_values_r <- estimatr::lm_robust(formula = wolak_score ~ Z,
                             data=results[results$full_cluster, ],
                             subset= pid2 == "R",
                             fixed_effects = ~block_id)
dem_values_d <- estimatr::lm_robust(formula = wolak_score ~ Z,
                             data=results[results$full_cluster, ],
                             subset= pid2 == "D",
                             fixed_effects = ~block_id)

# Outparty talk (exploratory)
talk_elec_r <- estimatr::lm_robust(formula = convo_elec_outparty_change ~ Z,
                             data=results[results$full_cluster, ],
                             subset= pid2 == "R",
                             fixed_effects = ~block_id)
talk_elec_d <- estimatr::lm_robust(formula = convo_elec_outparty_change ~ Z,
                             data=results[results$full_cluster, ],
                             subset= pid2 == "D",
                             fixed_effects = ~block_id)
```


## Figure 1

```{r}
# plot helper function
p_fx <- function(xlim = c(-1,1)){
  plot(x = 1,
     y = 1,
     type = "n",
     xlim = xlim,
     ylim = c(1.3,2.15),
     xaxs = "i",
     yaxs = "i",
     axes = F,
     xlab = "",
     ylab = "")
abline(v = 0, lwd = 1, lty  = 2, col = "grey40")
}

pdf("figure1.pdf", height = 2.25, width = 10)

layout(matrix(c(1,2,3,4,5), nrow = 1))

cex <- 1
cex_num <- 1.4
est_size <- 1.3
par(oma = c(5,6,1,0.5), mar = c(1,1,1,3.2), mgp=c(3,.5,0))

# H1 and H4
p_fx(xlim = c(-.2, 12))
mtext(side = 1, text = "Change in\noutparty affect\n(H1 & H3)", line = 5,cex =cex)
axis(side = 1, at = seq(0,12,3), labels = seq(0,12,3), cex.axis =cex_num, tck = -.03)

points(x = summary(h1_affect)$coef[1, "Estimate"],
       y = 2, pch = 16, cex = est_size)
segments(x0 = summary(h1_affect)$coef[1,"CI Lower"],
         x1 = summary(h1_affect)$coef[1,"CI Upper"],
         y0 = 2,lwd = est_size)

points(x = summary(cate_d_affect)$coef[1, "Estimate"],
       y = 1.75, pch = 16, cex = est_size)
segments(x0 = summary(cate_d_affect)$coef[1,"CI Lower"],
         x1 = summary(cate_d_affect)$coef[1,"CI Upper"],
         y0 = 1.75,lwd = est_size)

points(x = summary(cate_r_affect)$coef[1, "Estimate"],
       y = 1.5, pch = 16, cex = est_size)
segments(x0 = summary(cate_r_affect)$coef[1,"CI Lower"],
         x1 = summary(cate_r_affect)$coef[1,"CI Upper"],
         y0 = 1.5,lwd = est_size)

#round(h4_affect$p.value["Z:pid2R"], 2)
pBrackets::brackets(11.3, 1.8, 11.3, 1.45, lwd=1,
                    type = 4, h = .15, tick = NA)
text(x= 11.05, y = 1.625, labels = "p=.22", 
     xpd = T, cex = cex_num, pos = 4)

mtext(text = c("SATE", "Democrats", "Republicans"),
      side = 2, at = c(2, 1.75, 1.5), las = 1,cex =cex)
mtext(text = ~underline("Estimate"),font = 2,
      side = 2, cex =cex, line = 0, las = 1, at = 2.25)

# H2 and H5
p_fx(xlim = c(-.05, .25))
mtext(side = 1, text = "Change in social\npolarization\n(H2 & H4)",
      line = 5,cex =cex)
axis(side = 1, at = seq(-.05,.25,.1), cex.axis =cex_num,
     labels = c("-.05",  ".05",  ".15", ".25"), tck = -.03, )

points(x = summary(h2_sp)$coef[1, "Estimate"],
       y = 2, pch = 16, lwd = 2, cex = est_size)
segments(x0 = summary(h2_sp)$coef[1,"CI Lower"],
         x1 = summary(h2_sp)$coef[1,"CI Upper"],
         y0 = 2,lwd = est_size)

points(x = summary(cate_d_sp)$coef[1, "Estimate"],
       y = 1.75, pch = 16, cex = est_size)
segments(x0 = summary(cate_d_sp)$coef[1,"CI Lower"],
         x1 = summary(cate_d_sp)$coef[1,"CI Upper"],
         y0 = 1.75,lwd = est_size)

points(x = summary(cate_r_sp)$coef[1, "Estimate"],
       y = 1.5, pch = 16, cex = est_size)
segments(x0 = summary(cate_r_sp)$coef[1,"CI Lower"],
         x1 = summary(cate_r_sp)$coef[1,"CI Upper"],
         y0 = 1.5,lwd = est_size)

#round(h5_sp$p.value["Z:pid2R"], 2)
pBrackets::brackets(.203+.02, 1.8, .203+.02, 1.45,xpd = T, lwd=1, type = 4, h = .005, tick = NA)
text(x= .2+.02, y = 1.625, labels = "p=.75",  xpd = T, pos = 4,cex =cex_num)



# H3 and H6
p_fx(xlim = c(-.1, .2))
mtext(side = 1, text = "Change in perceptions\nof election integrity\n(H3 & H6)",
      line = 5,cex =cex)
axis(side = 1, at = seq(-.1,.2,.1),
     labels = c("-.10", "0", ".10",".20"), cex.axis =cex_num, tck = -.03)

points(x = summary(h3_elec)$coef[1, "Estimate"],
       y = 2, pch = 16, lwd = 2, cex = est_size)
segments(x0 = summary(h3_elec)$coef[1,"CI Lower"],
         x1 = summary(h3_elec)$coef[1,"CI Upper"],
         y0 = 2,lwd = est_size)

points(x = summary(cate_d_elec)$coef[1, "Estimate"],
       y = 1.75, pch = 16, cex = est_size)
segments(x0 = summary(cate_d_elec)$coef[1,"CI Lower"],
         x1 = summary(cate_d_elec)$coef[1,"CI Upper"],
         y0 = 1.75,lwd = est_size)

points(x = summary(cate_r_elec)$coef[1, "Estimate"],
       y = 1.5, pch = 16, cex = est_size)
segments(x0 = summary(cate_r_elec)$coef[1,"CI Lower"],
         x1 = summary(cate_r_elec)$coef[1,"CI Upper"],
         y0 = 1.5,lwd = est_size)

#round(h6_elec$p.value["Z:pid2R"], 2)
pBrackets::brackets(.15, 1.8, .15, 1.45, lwd=1, type = 4, h = .01, tick = NA)
text(x= .15, y = 1.625, labels = "p=.85",  xpd = T, cex =cex_num, pos = 4)


# Exploratory hypotheses (dem values)
p_fx(xlim = c(-.25, .5))
mtext(side = 1, text = "Support for\ndemocratic values\n(Exploratory)",
      line = 5, cex =cex)
axis(side = 1, at = seq(-.25,.5,.25),cex.axis =cex_num,
     labels =  c("-.25",   "0", '.25',  ".50"), tck = -.03)

points(x = summary(dem_values)$coef[1, "Estimate"],
       y = 2, pch = 16, lwd = 2, cex = est_size)
segments(x0 = summary(dem_values)$coef[1,"CI Lower"],
         x1 = summary(dem_values)$coef[1,"CI Upper"],
         y0 = 2,lwd = est_size)

points(x = summary(dem_values_d)$coef[1, "Estimate"],
       y = 1.75, pch = 16, cex = est_size)
segments(x0 = summary(dem_values_d)$coef[1,"CI Lower"],
         x1 = summary(dem_values_d)$coef[1,"CI Upper"],
         y0 = 1.75,lwd = est_size)

points(x = summary(dem_values_r)$coef[1, "Estimate"],
       y = 1.5, pch = 16, cex = est_size)
segments(x0 = summary(dem_values_r)$coef[1,"CI Lower"],
         x1 = summary(dem_values_r)$coef[1,"CI Upper"],
         y0 = 1.5,lwd = est_size)

#round(dem_values_int$p.value["Z:pid2R"], 2)
pBrackets::brackets(.47, 1.8, .47, 1.45,xpd = T, lwd=1, type = 4,
                    h = .02, tick = NA)
text(x= .465, y = 1.625, labels = "p=.10",  xpd = T, pos = 4,cex =cex_num)


# Exploratory hypotheses (talk to outparty)
p_fx(xlim = c(-.05, .4))
mtext(side = 1, text = "Change in willingness to\ntalk to outparty member\n(Exploratory)",
      line = 5,cex =cex)
axis(side = 1, at = seq(-.2,.4,.2),cex.axis =cex_num,
     labels =  c("", 0, ".20", ".40"), tck = -.03)

points(x = summary(talk_elec)$coef[1, "Estimate"],
       y = 2, pch = 16, cex = est_size)
segments(x0 = summary(talk_elec)$coef[1,"CI Lower"],
         x1 = summary(talk_elec)$coef[1,"CI Upper"],
         y0 = 2,lwd = est_size)

points(x = summary(talk_elec_d)$coef[1, "Estimate"],
       y = 1.75, pch = 16, cex = est_size)
segments(x0 = summary(talk_elec_d)$coef[1,"CI Lower"],
         x1 = summary(talk_elec_d)$coef[1,"CI Upper"],
         y0 = 1.75,lwd = est_size)

points(x = summary(talk_elec_r)$coef[1, "Estimate"],
       y = 1.5, pch = 16, cex = est_size)
segments(x0 = summary(talk_elec_r)$coef[1,"CI Lower"],
         x1 = summary(talk_elec_r)$coef[1,"CI Upper"],
         y0 = 1.5,lwd = est_size)

#round(talk_elec_int$p.value["Z:pid2R"], 2)
pBrackets::brackets(.47-.08, 1.8, .47-.08, 1.45,xpd = T,
                    lwd=1, type = 4, h = .02, tick = NA)
text(x= .465-.07, y = 1.625, labels = "p=.66",
     xpd = T, pos = 4,cex =cex_num)

dev.off()
```

## In manuscript text, pg. 6

```{r}
# outparty affect
effectsize::cohens_d(outparty_change ~ Z==0,
                     data = results[results$full_cluster, ],
                     pooled_sd = F)

# social polarization
effectsize::cohens_d(sp_change ~ Z==0,
                     data = results[results$full_cluster, ],
                     pooled_sd = F)
```



## In manuscript text, pg. 1, 6-7 (Full tables listed here are in Appendix N)

```{r}
maketable(
  list(
    h1_affect,
    h4_affect,
    h2_sp,
    h5_sp,
    h3_elec,
    h6_elec,
    dem_values,
    dem_values_int
  ),
  title = "In manuscript text, pg. 1, 6-7; Full tables in Appendix N, A.16"
)
```

```{r}
maketable(
  list(
    cate_d_affect,
    cate_r_affect,
    cate_d_sp,
    cate_r_sp,
    cate_d_elec,
    cate_r_elec,
    dem_values_d,
    dem_values_r
  ),
  title = "Appendix N, A.17"
)
```

# Appendix B

## In SI text, pg. 4 (also Manuscript, pg. 4)

```{r}
# Recruitment
nrow(results)

# Willing to return
sum(results$followup, na.rm = T)

# Included in design
sum(!is.na(results$Z))

# Individual completed convo or short essay
sum(results$completed_post)

# Partnership completed convo or short essay
sum(results$full_cluster)

# Equal numbers of Republicans and Democrats
# included in analyses
table(results$pid2[results$full_cluster])

# Completed in first 3 days of fielding
prop.table(table(results$date[results$full_cluster] %in% c("Feb 1", "Feb 2", "Feb 3")))

# Completes in first week
prop.table(table(results$date[results$full_cluster] %in% c("Feb 1", "Feb 2", "Feb 3",
                                                           "Feb 4", "Feb 5", "Feb 6",
                                                           "Feb 8")))

# Completed the followup 3 days later
sum(results$completed_followup[results$full_cluster])
```

## Table A.1

```{r}
# Creating a dataset at the partnership-level
partner_level <- results[!is.na(results$Z) ,c("full_cluster", "cluster_id", "Z")]

# remove duplicates b/c all partnership-level variables
partner_level <- unique(partner_level)

# outcome is whether the partnership is included in the analysis or not
# (and therefore finished the whole task)
posttest_attrition <- estimatr:: lm_robust(formula = full_cluster ~ Z,
                                           data = partner_level)

# subset is everyone included in the main analysis
# outcome is whether the individual returned for followup survey
followup_attrition <- estimatr:: lm_robust(formula = completed_followup ~ Z,
                                           data = results[results$full_cluster,])

# subset is everyone included in the main analysis
# outcome is whether the individual returned for followup survey
# also including effect of conversation
followup_attrition2 <- estimatr:: lm_robust(formula = completed_followup ~ Z + outparty_change,
                                   data = results[results$full_cluster,])

maketable(list(posttest_attrition,
               followup_attrition,
               followup_attrition2),
          title = "Table A.1")
```

# Appendix C

## Figure A.1

```{r}
# cluster-level summary statistics considering the entire design
design_df <- results %>%
  filter(!is.na(Z)) %>%
  group_by(cluster_id) %>%
  summarise(
    Z = unique(Z),
    outparty_ft_diff = abs(diff(outparty_pre)),
    outparty_ft_avg = mean(outparty_pre),
    sp_diff = abs(diff(sp_pre_score)),
    sp_avg = mean(sp_pre_score),
    inparty_ft_diff = abs(diff(inparty_pre)),
    inparty_ft_avg = mean(inparty_pre),
    n_strong_partisans = sum(pid3 == 2),
    elec_admin_avg = mean(election_admin_pre),
    elec_admin_diff = abs(diff(election_admin_pre)),
    avg_ap = mean(ap_pre),
    n_male = sum(gender == "Male"),
    n_white = sum(race_ethn_white == 1),
    age_diff = abs(diff(age)),
    age_avg = mean(age),
    educ_diff = abs(diff(education)),
    educ_avg = mean(education),
    convopref_diff = abs(diff(convopref_outparty_pre)),
    convopref_avg = mean(convopref_outparty_pre),
    outparty_mod_diff = abs(diff(outparty_mod_pre)),
    outparty_mod_avg = mean(outparty_mod_pre),
    outparty_ext_diff = abs(diff(outparty_ext_pre)),
    outparty_ext_avg = mean(outparty_ext_pre),
    ft_fec_diff = abs(diff(ft_fec_pre)),
    ft_fec_avg = mean(ft_fec_pre),
    ft_trump_diff = abs(diff(ft_trump_pre)),
    ft_trump_avg = mean(ft_trump_pre),
    ft_biden_diff = abs(diff(ft_biden_pre)),
    ft_biden_avg = mean(ft_biden_pre),
    polinterest_diff = abs(diff(polinterest)),
    polinterest_avg = mean(polinterest),
    polattn_diff = abs(diff(polattn)),
    polattn_avg = mean(polattn),
    moralize_diff = abs(diff(moralize)),
    moralize_avg = mean(moralize),
    n_election_fair = sum(election_fair == "Yes"),
    elec_pride_avg = mean(elec_pride_pre),
    elec_hope_avg = mean(elec_hope_pre),
    elec_anger_avg = mean(elec_anger_pre),
    elec_fear_avg = mean(elec_fear_pre),
    elec_transition_avg = mean(elec_transition_pre),
    elec_transition_diff = abs(diff(elec_transition_pre)),
    elec_trump_diff = abs(diff(elec_trump_pre)),
    elec_trump_avg = mean(elec_trump_pre),
    elec_biden_diff = abs(diff(elec_biden_pre)),
    elec_biden_avg = mean(elec_biden_pre),
    n_status_winner = sum(status_winner == "More like a winner"),
    n_status_parties = sum(status_parties == "Democrats"),
    n_vote_choice = sum(vote_choice == "BidenHarris")
  )

# cluster-level summary statistics for just those in analysis
analysis_df <- results %>%
  filter(full_cluster) %>%
  group_by(cluster_id) %>%
  summarise(
    Z = unique(Z),
    outparty_ft_diff = abs(diff(outparty_pre)),
    outparty_ft_avg = mean(outparty_pre),
    sp_diff = abs(diff(sp_pre_score)),
    sp_avg = mean(sp_pre_score),
    inparty_ft_diff = abs(diff(inparty_pre)),
    inparty_ft_avg = mean(inparty_pre),
    n_strong_partisans = sum(pid3 == 2),
    elec_admin_avg = mean(election_admin_pre),
    elec_admin_diff = abs(diff(election_admin_pre)),
    avg_ap = mean(ap_pre),
    n_male = sum(gender == "Male"),
    n_white = sum(race_ethn_white == 1),
    age_diff = abs(diff(age)),
    age_avg = mean(age),
    educ_diff = abs(diff(education)),
    educ_avg = mean(education),
    convopref_diff = abs(diff(convopref_outparty_pre)),
    convopref_avg = mean(convopref_outparty_pre),
    outparty_mod_diff = abs(diff(outparty_mod_pre)),
    outparty_mod_avg = mean(outparty_mod_pre),
    outparty_ext_diff = abs(diff(outparty_ext_pre)),
    outparty_ext_avg = mean(outparty_ext_pre),
    ft_fec_diff = abs(diff(ft_fec_pre)),
    ft_fec_avg = mean(ft_fec_pre),
    ft_trump_diff = abs(diff(ft_trump_pre)),
    ft_trump_avg = mean(ft_trump_pre),
    ft_biden_diff = abs(diff(ft_biden_pre)),
    ft_biden_avg = mean(ft_biden_pre),
    polinterest_diff = abs(diff(polinterest)),
    polinterest_avg = mean(polinterest),
    polattn_diff = abs(diff(polattn)),
    polattn_avg = mean(polattn),
    moralize_diff = abs(diff(moralize)),
    moralize_avg = mean(moralize),
    n_election_fair = sum(election_fair == "Yes"),
    elec_pride_avg = mean(elec_pride_pre),
    elec_hope_avg = mean(elec_hope_pre),
    elec_anger_avg = mean(elec_anger_pre),
    elec_fear_avg = mean(elec_fear_pre),
    elec_transition_avg = mean(elec_transition_pre),
    elec_transition_diff = abs(diff(elec_transition_pre)),
    elec_trump_diff = abs(diff(elec_trump_pre)),
    elec_trump_avg = mean(elec_trump_pre),
    elec_biden_diff = abs(diff(elec_biden_pre)),
    elec_biden_avg = mean(elec_biden_pre),
    n_status_winner = sum(status_winner == "More like a winner"),
    n_status_parties = sum(status_parties == "Democrats"),
    n_vote_choice = sum(vote_choice == "BidenHarris")
  )

# check variables are still in the same order
#all(colnames(analysis_df) == colnames(design_df))

# difference in means to check partnership-level balance across conditions
design_balance <- colnames(design_df)[-c(1:2)] %>%
  map(~ summary(estimatr::difference_in_means(as.formula(paste(.x, "~ Z")), data = design_df))) %>%
  map(~.$coefficients) %>%
  map_dfr(~as_tibble(.))

analysis_balance <- colnames(analysis_df)[-c(1:2)] %>%
  map(~ summary(estimatr::difference_in_means(as.formula(paste(.x, "~ Z")), data = analysis_df))) %>%
  map(~.$coefficients) %>%
  map_dfr(~as_tibble(.)) 
                                               

rownames(design_balance) <- rownames(analysis_balance) <- c(
  "Outparty feeling thermometer -- abs diff",
  "Outparty feeling thermometer -- mean",
  "Social polarization -- abs diff",
  "Social polarization -- mean",
  "Inparty feeling thermometer -- abs diff",
  "Inparty feeling thermometer -- mean",
  "Number strong partisans",
  "Election administration -- mean",
  "Election administration -- abs diff",
  "Affective polarization -- mean",
  "Number men",
  "Number nonwhite",
  "Age -- difference",
  "Age -- mean",
  "Education -- difference",
  "Education -- mean",
  "Preference for interparty contact -- difference",
  "Preference for interparty contact -- mean",
  "Outparty moderate, inactive feeling thermometer -- difference",
  "Outparty moderate, inactive feeling thermometer -- mean",
  "Outparty extreme, active feeling thermometer -- difference",
  "Outparty extreme, active feeling thermometer -- mean",
  "FEC, active feeling thermometer -- difference",
  "FEC, active feeling thermometer -- mean",
  "Trump feeling thermometer -- difference",
  "Trump feeling thermometer -- mean",
  "Biden, active feeling thermometer -- difference",
  "Biden, active feeling thermometer -- mean",
  "Political interest -- difference",
  "Political interest -- mean",
  "Political attention -- difference",
  "Political attention -- mean",
  "Moralize partisanship -- difference",
  "Moralize partisanship -- mean",
  "Number felt election fair",
  "Pride -- mean",
  "Hope -- mean",
  "Anger -- mean",
  "Fear -- mean",
  "Smooth transition -- mean",
  "Smooth transition -- difference",
  "Trump conduct -- difference",
  "Trump conduct -- mean",
  "Biden conduct -- difference",
  "Biden conduct -- mean",
  "Number that felt like a winner",
  "Number that felt Democrats higher status",
  "Number that voted Biden, Harris"
)

```


```{r}
o <- order(analysis_balance$`Pr(>|t|)` + design_balance$`Pr(>|t|)`)
pdf("figureA1.pdf", width = 6, height = 8)
par(mar = c(3,14,3,3))
plot(y = 1,
     x = 1,
     xlim = c(0,1),
     ylim = c(.5, nrow(analysis_balance) + .5),
     type = "n",
     xlab = "",
     ylab = "",
     axes = F,
     xaxs = "i",
     yaxs = "i")
mtext(side = 1, line = 1.25, text = "p-value", cex = .75)
abline(h = 1:nrow(analysis_balance), col = "grey80")
abline(v=.05, col = "grey80", lty = "dashed")
points(y = 1:nrow(analysis_balance),
       x = analysis_balance$`Pr(>|t|)`[o],
       pch = 16,
       col = "black",
       xpd = T)
points(y = 1:nrow(analysis_balance),
       x = design_balance$`Pr(>|t|)`[o],
       pch = 18,
       col = "grey40",
       xpd = T)
axis(side = 2,
     at = 1:nrow(analysis_balance),
     labels = rownames(analysis_balance)[o],
     line = -.5,
     las = 2,
     cex.axis = .6,
     xpd = T,
     tick = F)
axis(side = 1,
     labels = F,
     xpd = T)
mtext(side = 1,
      at = seq(0,1,.2),
      text = seq(0,1,.2),
      cex = .75,
      line = .5)
l<-legend("bottomright",
       bty = "n",
       cex = .75,
       pt.cex = 1,
       pch = c(18,16),
       col = c("grey40", "black"),
       legend = c("Design Sample", "Analysis Sample"))
dev.off()
```




# Appendix F

## Table A.2

```{r}
# Extreme, active outpartisans
ap_ext_act <- estimatr::lm_robust(formula = outparty_ext_change ~ Z,
                                  data = results[results$full_cluster, ],
                                  clusters = cluster_id_se,
                                  fixed_effects = ~block_id)

ap_ext_act_hte <- estimatr::lm_robust(formula = outparty_ext_change ~ Z*pid2,
                                      data = results[results$full_cluster, ],
                                      clusters = cluster_id_se,
                                      fixed_effects = ~block_id)

# Moderate, inactive outpartisans
ap_mod_inact <- estimatr::lm_robust(formula = outparty_mod_change ~ Z,
                                    data = results[results$full_cluster, ],
                                    clusters = cluster_id_se,
                                    fixed_effects = ~block_id)

ap_mod_inact_hte <- estimatr::lm_robust(formula = outparty_mod_change ~ Z*pid2,
                                        data = results[results$full_cluster, ],
                                        clusters = cluster_id_se,
                                        fixed_effects = ~block_id)

maketable(
  list(
    h1_affect,
    h4_affect,
    ap_mod_inact,
    ap_mod_inact_hte,
    ap_ext_act,
    ap_ext_act_hte
  ),
  title = "Table A.2"
)
```

# Appendix G

## Table A.3

```{r}
# Main results with outcome defined as inparty affect - outparty affect
h1_ap <- estimatr::lm_robust(formula = ap_change ~ Z,
                            data = results[results$full_cluster, ],
                            fixed_effects = ~block_id,
                            clusters = cluster_id_se)

h4_ap <- estimatr::lm_robust(formula = ap_change ~ Z*pid2, 
                            data = results[results$full_cluster, ],
                            clusters = cluster_id_se,
                            fixed_effects = ~block_id)

maketable(
  list(
    h1_ap,
    h4_ap
  ),
  title = "Table A.3"
)
```


# Appendix H

```{r}
# Equivalence tests
coef_int <- coef(h4_affect)["Z:pid2R"]
se_int <- summary(h4_affect)$coefficients["Z:pid2R", "Std. Error"]

delta <- 4

t1 <- (delta - coef_int)/se_int
t2 <- (coef_int + delta)/se_int

pnorm(t1, lower.tail = F)
pnorm(t2, lower.tail = F)

delta <- 6.05

t1 <- (delta - coef_int)/se_int
t2 <- (coef_int + delta)/se_int

pnorm(t1, lower.tail = F)
pnorm(t2, lower.tail = F)
```

# Appendix I

## Table A.4

See "Descriptive statistics in 'Partisanship and Threat' section" of replication file

```{r}
# difference in proportions test
wl_tab <- table(results$status_winner[results$full_cluster],
                results$pid2[results$full_cluster],
                useNA = "ifany")
prop.test(wl_tab[2,], n = colSums(wl_tab))
```


## Table A.5

See "Descriptive statistics in 'Partisanship and Threat' section" of replication file

```{r}
# difference in proportions test
party_status_tab <- table(results$status_parties[results$full_cluster],
                          results$pid2[results$full_cluster],
                          useNA = "ifany")
prop.test(c(23, 289-23), n = colSums(party_status_tab))
```


## Table A.6

```{r}
# helper function
emotion_prop_test <- function(v){
  emotion_tab <- table(results[results$full_cluster, v],
                       results[results$full_cluster, "pid2"], useNA = "ifany")
  #print(emotion_tab)
  prop.test(x = emotion_tab[2,], n = colSums(emotion_tab))
}

# proportions in table
emotion_prop_test("status_feel_threatened")
emotion_prop_test("status_feel_anxious")
emotion_prop_test("status_feel_angry")
emotion_prop_test("status_feel_afraid")
emotion_prop_test("status_feel_disgusted")

emotion_prop_test("status_feel_enthusiastic")
emotion_prop_test("status_feel_happy")
emotion_prop_test("status_feel_relieved")
emotion_prop_test("status_feel_hopeful")
emotion_prop_test("status_feel_reassured")

# summing self-reports of each emotion
results$threat_sum <- rowSums(results[,c("status_feel_threatened",
                                         "status_feel_anxious",
                                         "status_feel_angry",
                                         "status_feel_disgusted",
                                         "status_feel_afraid")], na.rm = T)
results$reassure_sum <- rowSums(results[,c("status_feel_enthusiastic",
                                         "status_feel_happy",
                                         "status_feel_relieved",
                                         "status_feel_hopeful",
                                         "status_feel_reassured")], na.rm = T)

# final two rows in table
prop.table(table((results$threat_sum > 0)[results$full_cluster],
                 results$pid2[results$full_cluster]),
           margin = 2)
prop.table(table((results$reassure_sum > 0)[results$full_cluster],
                 results$pid2[results$full_cluster]),
           margin = 2)
```




## Table A.7

```{r}
prop.table(table(results$vote_choice[results$full_cluster],
                 results$pid2[results$full_cluster]),
           margin = 2)
```


# Appendix J

## Table A.8

```{r}
# These models change the operationalization of threat
# from partisanship to another measure and rerun our main analyses

# Winner/loser status
h4_winner_cond <- estimatr:: lm_robust(formula = outparty_change ~ Z*(status_winner=="More like a loser"),
                                         data = results[results$full_cluster, ],
                                         clusters = cluster_id_se,
                                         fixed_effects = ~block_id)

h5_winner_cond <- estimatr:: lm_robust(formula = sp_change ~ Z*(status_winner=="More like a loser"),
                                         data = results[results$full_cluster, ],
                                         clusters = cluster_id_se,
                                         fixed_effects = ~block_id)

h6_winner_cond <- estimatr:: lm_robust(formula = elec_change ~ Z*(status_winner=="More like a loser"),
                                         data = results[results$full_cluster, ],
                                         clusters = cluster_id_se,
                                         fixed_effects = ~block_id)

wolak_hte_winner_cond  <-  estimatr:: lm_robust(formula = wolak_score ~ Z*(status_winner=="More like a loser"),
                                         data = results[results$full_cluster, ],
                                         clusters = cluster_id_se,
                                         fixed_effects = ~block_id)

# Status of the parties

# outparty has higher status = 1 = threat
# inparty has higher status = 0
results$partystatus_dummy <- ifelse(results$status_parties == "Democrats" & results$pid2 == "R", 1,
                  ifelse(results$status_parties == "Republicans" & results$pid2 == "D", 1, 0))

h4_demhigher_cond <- estimatr:: lm_robust(formula = outparty_change ~ Z*partystatus_dummy,
                                         data = results[results$full_cluster, ],
                                         clusters = cluster_id_se,
                                         fixed_effects = ~block_id)

h5_demhigher_cond <- estimatr:: lm_robust(formula = sp_change ~ Z*partystatus_dummy,
                                         data = results[results$full_cluster, ],
                                         clusters = cluster_id_se,
                                         fixed_effects = ~block_id)

h6_demhigher_cond <- estimatr:: lm_robust(formula = elec_change ~ Z*partystatus_dummy,
                                         data = results[results$full_cluster, ],
                                         clusters = cluster_id_se,
                                         fixed_effects = ~block_id)

wolak_hte_demhigher_cond  <-  estimatr:: lm_robust(formula = wolak_score ~ Z*partystatus_dummy,
                                         data = results[results$full_cluster, ],
                                         clusters = cluster_id_se,
                                         fixed_effects = ~block_id)

# Threat/reassurance emotions
results$more_threat_dummy <- ifelse(results$threat_sum > results$reassure_sum, 1, 0)

h4_threat_cond <- estimatr:: lm_robust(formula = outparty_change ~ Z*more_threat_dummy,
                                         data = results[results$full_cluster, ],
                                         clusters = cluster_id_se,
                                         fixed_effects = ~block_id)

h5_threat_cond <- estimatr:: lm_robust(formula = sp_change ~ Z*more_threat_dummy,
                                         data = results[results$full_cluster, ],
                                         clusters = cluster_id_se,
                                         fixed_effects = ~block_id)

h6_threat_cond <- estimatr:: lm_robust(formula = elec_change ~ Z*more_threat_dummy,
                                         data = results[results$full_cluster, ],
                                         clusters = cluster_id_se,
                                         fixed_effects = ~block_id)

wolak_hte_threat_cond  <-  estimatr:: lm_robust(formula = wolak_score ~ Z*more_threat_dummy,
                                         data = results[results$full_cluster, ],
                                         clusters = cluster_id_se,
                                         fixed_effects = ~block_id)

maketable(
  list(
    h4_winner_cond,
    h5_winner_cond,
    h6_winner_cond,
    wolak_hte_winner_cond,
    h4_demhigher_cond,
    h5_demhigher_cond,
    h6_demhigher_cond,
    wolak_hte_demhigher_cond,
    h4_threat_cond,
    h5_threat_cond,
    h6_threat_cond,
    wolak_hte_threat_cond
  ),
  title = "Table A.8"
) %>%
  kableExtra::landscape()
```


## Table A.9

```{r}
results$vote_dummy <- ifelse(results$vote_choice == "TrumpPence" & results$pid2 == "R", 1,
                             ifelse(results$vote_choice == "BidenHarris" & results$pid2 == "D", 0, NA))

h4_vote_cond <- estimatr:: lm_robust(formula = outparty_change ~ Z*vote_dummy,
                                         data = results[results$full_cluster, ],
                                         clusters = cluster_id_se,
                                         fixed_effects = ~block_id)

h5_vote_cond <- estimatr:: lm_robust(formula = sp_change ~ Z*vote_dummy,
                                         data = results[results$full_cluster, ],
                                         clusters = cluster_id_se,
                                         fixed_effects = ~block_id)

h6_vote_cond <- estimatr:: lm_robust(formula = elec_change ~ Z*vote_dummy,
                                         data = results[results$full_cluster, ],
                                         clusters = cluster_id_se,
                                         fixed_effects = ~block_id)

wolak_vote_cond  <-  estimatr:: lm_robust(formula = wolak_score ~ Z*vote_dummy,
                                         data = results[results$full_cluster, ],
                                         clusters = cluster_id_se,
                                         fixed_effects = ~block_id)

maketable(list(h4_vote_cond,
               h5_vote_cond,
               h6_vote_cond,
               wolak_vote_cond),
          title = "A.9")
```

# Appendix K

## Figure A.4

```{r}
# transforming data to long format
df_long <- results[results$full_cluster, c("Z", "outparty_pre", "outparty_post",
                                     "sp_pre_score", "sp_post_score")] %>%
  pivot_longer(cols = -c("Z"),
               names_to = "outcome",
               values_to = "y") %>%
  mutate(time_pre = grepl("_pre", outcome)) %>%
  mutate(Z = ifelse(Z == 1, "Conversation (Treatment)", "Short Essay (Control)")) %>%
  mutate(dv = ifelse(grepl("sp", outcome), "Social Polarization Scale",
                     "Outparty Feeling Thermometer"), .keep = "unused")

# calculating 95% CI for the sample mean assuming CLT
condition_CIs <- df_long %>%
  group_by(Z, time_pre, dv) %>%
  summarise(ci = list(mean_cl_normal(y) %>%
                        rename(mean_y = y,
                               lwr = ymin,
                               upr = ymax)),
            .groups = "drop_last") %>%
  unnest(cols = c(ci))

# x-axis limits
condition_CIs$xmin <- ifelse(condition_CIs$dv == "Outparty Feeling Thermometer", 25, 1.5)
condition_CIs$xmax <- ifelse(condition_CIs$dv == "Outparty Feeling Thermometer", 40, 2)


# plot
p <- ggplot(data = condition_CIs, aes(x = as.numeric(time_pre),
                                      y = mean_y,
                                      ymin = lwr,
                                      ymax = upr)) +
  geom_point() +
  geom_errorbar(width=.2) +
  coord_flip() +
  scale_x_continuous(limits = c(-.5, 1.5), breaks = c(0,1),
                     labels = c("Post-treatment", "Pre-treatment")) +
  # Facet wrapped with one column using group 2s
  facet_wrap(~dv*Z, ncol = 1,scales = "free_x") +
  geom_blank(aes(y = xmin)) +
  geom_blank(aes(y = xmax)) +
  xlab("") +
  ylab("") +
  theme(axis.ticks.y = element_blank(),
        panel.grid.minor = element_blank()) 
pdf("figureA4.pdf", width = 5, height = 7)
p
dev.off()
```


## Table A.10

```{r, results='asis'}
ft_z1 <- t.test(results$outparty_post[results$full_cluster & results$ Z == 1],
           results$outparty_pre[results$full_cluster & results$ Z == 1],
           paired = T)
ft_z0 <- t.test(results$outparty_post[results$full_cluster & results$ Z == 0],
           results$outparty_pre[results$full_cluster & results$ Z == 0],
           paired = T)
sp_z1 <- t.test(results$sp_post_score[results$full_cluster & results$ Z == 1],
           results$sp_pre_score[results$full_cluster & results$ Z == 1],
           paired = T)
sp_z0 <- t.test(results$sp_post_score[results$full_cluster & results$ Z == 0],
           results$sp_pre_score[results$full_cluster & results$ Z == 0],
           paired = T)
# N for table
length(results$outparty_post[results$full_cluster & results$ Z == 1])
length(results$outparty_post[results$full_cluster & results$ Z == 0])
length(results$sp_pre_score[results$full_cluster & results$ Z == 1])
length(results$sp_pre_score[results$full_cluster & results$ Z == 0])

paired_ttests <- t(data.frame(ft_z1 = c(ft_z1$estimate, ft_z1$stderr, ft_z1$statistic,
                                   ft_z1$p.value, ft_z1$conf.int[1], ft_z1$conf.int[2]),
                            ft_z0 = c(ft_z0$estimate, ft_z0$stderr, ft_z0$statistic,
                                   ft_z0$p.value, ft_z0$conf.int[1], ft_z0$conf.int[2]),
                            sp_z1 = c(sp_z1$estimate, sp_z1$stderr, sp_z1$statistic,
                                   sp_z1$p.value, sp_z1$conf.int[1], sp_z1$conf.int[2]),
                            sp_z0 = c(sp_z0$estimate, sp_z0$stderr, sp_z0$statistic,
                                   sp_z0$p.value, sp_z0$conf.int[1], sp_z0$conf.int[2])))
colnames(paired_ttests) <- c("Estimate", "Std. Error", "t-statistic",
                             "p-value", "95% CI", "95% CI")
xtable::xtable(paired_ttests)
```

# Appendix L

## In SI text, pg. 37

```{r}
# Did D's and R's participate at similar rates in conversation?
# In appendix prose
t.test(nturns_person ~ pid2,
       data = results[results$full_cluster & results$Z == 1, ])
```

## Table A.11

```{r}
# standardizing variable
results$nturns_std <- ((results$nturns_person - mean(results$nturns_person, na.rm = T)) 
                            / sd(results$nturns_person, na.rm = T))
  
mod_longer <- estimatr:: lm_robust(formula = outparty_change ~ nturns_std,
                                     data = results[results$full_cluster,],
                                     clusters = cluster_id_se,
                                     fixed_effects = ~block_id)
mod_longer_person_int <- estimatr:: lm_robust(formula = outparty_change ~ Z*nturns_std,
                                     data = results[results$full_cluster,],
                                     clusters = cluster_id_se,
                                     fixed_effects = ~block_id)

maketable(list(mod_longer,
               mod_longer_person_int),
          title = "A.11")
```


## Figure A.5

```{r}
disclosure <- lm(outparty_change ~ mechanism_disclosure,
                 data = results[results$full_cluster == T, ])
empathy <- lm(outparty_change ~ mechanism_empathy,
              data = results[results$full_cluster == T, ])
learn <- lm(outparty_change ~ outparty_learn,
            data = results[results$full_cluster == T, ])

modlist_mech <- list(disclosure, empathy, learn)
names(modlist_mech) <- c("Self-Disclosure", "Empathy", "Learned About Outparty")

pdf("figureA5.pdf", height = 3)
modelsummary::modelplot(
  modlist_mech,
  coef_omit = c("(Intercept)"),
  coef_map = c(
    "mechanism_disclosure" = "",
    "mechanism_empathy" = "",
    "outparty_learn" = ""
  ),
  facet = T
) +
  labs(x = "Coefficient (Change in Outparty Affect)\nand 95% confidence intervals",
       size = .5)
dev.off()
```


## Table A.12

```{r}
mod_talkmore <- estimatr:: lm_robust(formula = outparty_change ~ talkmore,
                                     data = results[results$full_cluster,],
                                     clusters = cluster_id_se,
                                     fixed_effects = ~block_id)

mod_talkmore_int <- estimatr:: lm_robust(formula = outparty_change ~ Z*talkmore,
                                         data = results[results$full_cluster,],
                                         clusters = cluster_id_se,
                                         fixed_effects = ~block_id)

maketable(list(mod_talkmore,
               mod_talkmore_int),
          title = "A.12")
```

## In SI text, pg. 40

```{r}
mod_pos_exp <- estimatr:: lm_robust(formula = outparty_change ~ convo_valence == "Positive/good",
                                         data = results[results$full_cluster,],
                                         clusters = cluster_id_se)
summary(mod_pos_exp)
```



## In SI text, pg. 40

```{r}
# select only the "negative" and "positive" categories
data_dictionary_LSD2015_pos_neg <- quanteda::data_dictionary_LSD2015[1:2]
unique_ids <- results$id[results$Z == 1 & results$full_cluster]
out <- data.frame(pos_start = rep(NA, (length(unique_ids))),
                  pos_end = rep(NA, (length(unique_ids))),
                  neg_start = rep(NA, (length(unique_ids))),
                  neg_end = rep(NA, (length(unique_ids))),
                  length_start = rep(NA, (length(unique_ids))),
                  length_end = rep(NA, (length(unique_ids))),
                  id = rep(NA, (length(unique_ids))))
for(i in 1:length(unique_ids)){
  out$id[i] <- unique_ids[i]
  c <- quanteda::corpus(unlist(results[out$id[i], c("mechanisms_open_start",
                                                    "mechanisms_open_end")]))
  c_t <- quanteda::tokens(c, remove_punct = T, remove_numbers = T)
  posneg <- quanteda::tokens_lookup(c_t, dictionary = data_dictionary_LSD2015_pos_neg)
  out$length_start[i] <- length(c_t[[1]])
  out$length_end[i] <- length(c_t[[2]])
  out$pos_start[i] <- sum(posneg[[1]] == "positive")
  out$pos_end[i] <- sum(posneg[[2]] == "positive")
  out$neg_start[i] <- sum(posneg[[1]] == "negative")
  out$neg_end[i] <- sum(posneg[[2]] == "negative")
}

# Number strictly more positive than negative at "start"
prop.table(table(out$pos_start/out$length_start > out$neg_start/out$length_start))

# Number strictly more positive than negative at "end"
prop.table(table(out$pos_end/out$length_end > out$neg_end/out$length_end))


sent_change <- ((out$pos_end - out$neg_end)/out$length_end) - 
               ((out$pos_start - out$neg_start)/out$length_start)

# Positive experiences more associated with positive affect
mod <- estimatr::lm_robust(results$outparty_change[results$Z == 1 & results$full_cluster] ~ sent_change,
                  cluster = results$cluster_id[results$Z == 1 & results$full_cluster])
summary(mod)
```





## In SI text, pg. 41

```{r echo=FALSE, eval=TRUE}
# Create a variable for the percentage of positive and negative words 
results$pos_room_pct <- results$pos_room/results$nwords_room
results$neg_room_pct <- results$neg_room/results$nwords_room

affect_pos_words_pct <- estimatr:: lm_robust(formula = outparty_change ~ pos_room_pct*Z,
                                     data = results[results$full_cluster,],
                                     clusters = cluster_id_se,
                                     fixed_effects = ~block_id)
affect_neg_words_pct <- estimatr:: lm_robust(formula = outparty_change ~ neg_room_pct*Z,
                                     data = results[results$full_cluster,],
                                     clusters = cluster_id_se,
                                     fixed_effects = ~block_id)

# Room-level sentiment analysis effect on affect
summary(affect_pos_words_pct)
summary(affect_neg_words_pct)
```


```{r echo=FALSE, eval=TRUE}
# See if the amount of contact (number of messages sent) was associated with outparty affect 
affect_nturns <- estimatr:: lm_robust(formula = outparty_change ~ nturns_room*Z,
                                     data = results[results$full_cluster,],
                                     clusters = cluster_id_se,
                                     fixed_effects = ~block_id)
summary(affect_nturns)
```


## In SI text, pg. 42

```{r}
# average words per room by condition
tapply(results$nwords_room, results$Z, mean, na.rm = T)
# average words per person by condition
tapply(results$nwords_person, results$Z, mean, na.rm = T)
```

## Table A.13

```{r}
# Per room, was topic discussed at all
results <- results[results$full_cluster, ] %>%
  # four prompted topics
  mutate(txt_actions_dummy = txt_actions_room > 0) %>%
  mutate(txt_transition_dummy = txt_transition_room > 0) %>%
  mutate(txt_fair_dummy = txt_fair_room > 0) %>%
  mutate(txt_state_dummy = txt_state_room > 0) %>%
  # two events
  mutate(txt_inaug_dummy = txt_inaug_room > 0) %>%
  mutate(txt_jan6_dummy = txt_jan6_room > 0) 

four_topics <-  c("txt_actions_dummy", "txt_transition_dummy",
                  "txt_fair_dummy", "txt_state_dummy")

# Number of topics each essay engaged with
round(prop.table(table(rowSums(results[results$full_cluster & results$Z == 0,four_topics]))),2)
# Number of topics each conversation engaged with
round(prop.table(table(rowSums(results[results$full_cluster & results$Z == 1,four_topics]))),2)
```

## Figure A.6

```{r}
# Plot of which topics
tab_actions <- data.frame(prop.table(table(results$txt_actions_dummy, results$Z),
                                     margin = 2),
                          topic = "Candidates'\nactions",
                          type = "Topic")
tab_transition <- data.frame(prop.table(table(results$txt_transition_dummy, results$Z),
                                        margin = 2),
                          topic = "Transition to\nBiden admin",
                          type = "Topic")
tab_fair <- data.frame(prop.table(table(results$txt_fair_dummy, results$Z),
                                  margin = 2),
                       topic = "Election\nfair",
                       type = "Topic")
tab_state <- data.frame(prop.table(table(results$txt_state_dummy, results$Z),
                                   margin = 2),
                        topic = "State of the\ncountry",
                        type = "Topic")
# Events
tab_inaug <- data.frame(prop.table(table(results$txt_inaug_dummy, results$Z),
                                   margin = 2),
                          topic = "Inauguration",
                        type = "Event")
tab_jan6 <- data.frame(prop.table(table(results$txt_jan6_dummy, results$Z),
                                  margin = 2),
                          topic = "January 6",
                       type = "Event")

tab <- rbind.data.frame(tab_actions,
                        tab_transition,
                        tab_fair,
                        tab_state,
                        tab_inaug,
                        tab_jan6)

colnames(tab) <- c("Discussed_topic",
                   "Z",
                   "Freq",
                   "Topic",
                   "type")
tab$type <- factor(tab$type, levels = c("Topic", "Event"))
p <- ggplot(tab[tab$Discussed_topic == T, ],
            aes(fill=Z, y=Freq, x=reorder(Topic, Freq))) + 
  geom_bar(width = 0.8, stat = "identity",
           position = position_dodge2(width = 0.8, preserve = "single")) +
  ylab("Discussed the Subtopic (%)") +
  xlab("") +
  facet_grid(cols = vars(type), scales = "free", space = "free") +
  scale_fill_grey(name = "", labels = c("Short Essay", "Conversation")) +
  theme_bw() +
  theme(text = element_text(size = 18),
        legend.position = "bottom",
        legend.margin=margin(t=-1, r=0, b=0, l=0, unit="cm"))

pdf("figureA6.pdf", width = 10, height = 5)
p
dev.off()
```


## Table A.14

```{r}
# Per topic, effect of conversation conditional on
# the conversation/essay talking about each election subtopic
# (descriptive, suffers post-treatment bias)
affect_actions <- estimatr::lm_robust(formula = outparty_change ~ Z,
                           data=results[results$full_cluster & results$txt_actions_dummy,],
                           clusters = cluster_id_se,
                           fixed_effects = ~block_id)

affect_state <- estimatr::lm_robust(formula = outparty_change ~ Z,
                           data = results[results$full_cluster & results$txt_state_dummy,],
                           clusters = cluster_id_se,
                           fixed_effects = ~block_id)

affect_transition <- estimatr::lm_robust(formula = outparty_change ~ Z,
                           data = results[results$full_cluster & results$txt_transition_dummy,],
                           clusters = cluster_id_se,
                           fixed_effects = ~block_id)

affect_fair <- estimatr::lm_robust(formula = outparty_change ~ Z,
                           data = results[results$full_cluster & results$txt_fair_dummy,],
                           clusters = cluster_id_se,
                           fixed_effects = ~block_id)

# Events
# (Not enough people talked about inauguration to estimate model)
affect_jan6 <- estimatr::lm_robust(formula = outparty_change ~ Z,
                            data = results[results$full_cluster & results$txt_jan6_dummy,],
                            clusters = cluster_id_se,
                            fixed_effects = ~block_id)

maketable(list(
  affect_actions,
  affect_state,
  affect_transition,
  affect_fair,
  affect_jan6
), title = "A.14")
```

## In SI text, pg. 45 

```{r}
elec_fair <- estimatr::lm_robust(formula = elec_change ~ Z,
                           data = results[results$full_cluster & results$txt_fair_dummy,],
                           clusters = cluster_id_se,
                           fixed_effects = ~block_id)
summary(elec_fair)
```




# Appendix M

## Table A.15

```{r}
affect_dur <- estimatr:: lm_robust(formula = outparty_change_t2 ~ Z,
                                   data = results[results$full_cluster,],
                                   clusters = cluster_id_se,
                                   fixed_effects = ~block_id)

affect_dur_int <- estimatr:: lm_robust(formula = outparty_change_t2 ~ Z*pid2,
                                   data = results[results$full_cluster,],
                                   clusters = cluster_id_se,
                                   fixed_effects = ~block_id)

# effect size
effectsize::cohens_d(outparty_change_t2 ~ Z==0,
                     data = results[results$full_cluster, ],
                     pooled_sd = F)

maketable(list(affect_dur,
               affect_dur_int),
          title = "A.15")
```


## Appendix N

## Table A.16

See Section 'Main Analyses for Figure 1' of this replication file


## Table A.17

See Section 'Main Analyses for Figure 1' of this replication file



# Appendix O

## In SI text, pg. 49

```{r}
# analysis sample, willingness to talk
table(results$convopref_outparty_pre[results$full_cluster])
prop.table(table(results$convopref_outparty_pre[results$full_cluster]))


# full design sample, willingness to talk
prop.table(table(results$convopref_outparty_pre[results$in_design]))
```

## Table A.18

```{r}
affect_pref <- estimatr:: lm_robust(formula = outparty_change ~ Z*convopref_outparty_pre, 
                            data = results[results$full_cluster, ],
                            clusters = cluster_id_se,
                            fixed_effects = ~block_id)

maketable(list(affect_pref), title = "A.18")
```

# Appendix P

## Table A.19

```{r}
# talk about 2020 with outparty member
# larger number = more willing
talk_elec <- estimatr:: lm_robust(formula = convo_elec_outparty_change ~ Z,
                                   data = results[results$full_cluster,],
                                   clusters = cluster_id_se,
                                   fixed_effects = ~block_id)

talk_elec_hte <- estimatr:: lm_robust(formula = convo_elec_outparty_change ~ Z*pid2,
                                   data = results[results$full_cluster,],
                                   clusters = cluster_id_se,
                                   fixed_effects = ~block_id)

# talk about politics with outparty member
# larger number = more willing
talkpol <- estimatr:: lm_robust(formula = talkpol_outparty_change ~ Z,
                                   data = results[results$full_cluster,],
                                   clusters = cluster_id_se,
                                   fixed_effects = ~block_id)

talkpol_hte <- estimatr:: lm_robust(formula = talkpol_outparty_change ~ Z*pid2,
                                   data = results[results$full_cluster,],
                                   clusters = cluster_id_se,
                                   fixed_effects = ~block_id)

# talk about sports and pop culture with outparty member
# larger number = more willing
talknonpol <- estimatr:: lm_robust(formula = talknonpol_outparty_change ~ Z,
                                   data = results[results$full_cluster,],
                                   clusters = cluster_id_se,
                                   fixed_effects = ~block_id)

talknonpol_hte <- estimatr:: lm_robust(formula = talknonpol_outparty_change ~ Z*pid2,
                                   data = results[results$full_cluster,],
                                   clusters = cluster_id_se,
                                   fixed_effects = ~block_id)


maketable(list(
  talk_elec,
  talk_elec_hte,
  talkpol,
  talkpol_hte,
  talknonpol,
  talknonpol_hte
), title = "A.19")

# Effect sizes 
effectsize::cohens_d(convo_elec_outparty_change ~ Z==0,
                     data = results[results$full_cluster, ],
                     pooled_sd = F)

effectsize::cohens_d(talkpol_outparty_change ~ Z==0,
                     data = results[results$full_cluster, ],
                     pooled_sd = F)

effectsize::cohens_d(talknonpol_outparty_change ~ Z==0,
                     data = results[results$full_cluster, ],
                     pooled_sd = F)

```




